Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
When this line of code is executed, the corresponding Property Get procedure is executed. Property Get procedures are actually functions, returning a value. In this case, code in the procedure simply returns the value stored in the internal private variable where the value property is stored. What is the point of using Property Get and Property Let procedures in a class? Why not simply declare one public variable for each property, so code outside the class could access that variable directly? The reason for doing things in what may seem to be an unnecessarily complicated manner is a principal called encapsulation. This principal states that the internal data and workings of a class should be as isolated as possible from the outside world. By requiring that access to a class’s properties be done via property procedures, Visual Basic gives the programmer the opportunity to provide whatever degree of encapsulation is required. We’ll see this principal in action as we continue to work on our SuperString class. Listing 6.1 Code created by the Class Builder utility for the SuperString class.
    Option Explicit

    ‘local variable(s) to hold property value(s)
    Private mvarValue As String ‘local copy
    Private mvarLength As Long ‘local copy

    Public Sub Insert(Txt As String, Position As Long)
    End Sub

    Public Property Let Length(ByVal vData As Long)
    ‘used when assigning a value to the property, on the left side of an assignment.
    ‘Syntax: X.Length = 5
        mvarLength = vData
    End Property

    Public Property Get Length() As Long
    ‘used when retrieving value of a property, on the right side of an assignment.
    ‘Syntax: Debug.Print X.Length
        Length = mvarLength
    End Property

    Public Property Let Value(ByVal vData As String)
    ‘used when assigning a value to the property, on the left side of an assignment.
    ‘Syntax: X.Value = 5
        mvarValue = vData
    End Property

    Public Property Get Value() As String
    ‘used when retrieving value of a property, on the right side of an assignment.
    ‘Syntax: Debug.Print X.Value
        Value = mvarValue
    End Property
  What modifications will we need to make to this code? Consider these three points: • First is the Value property, which stores the string. We want the user of the class to be able to get and to set this value. There are no restrictions on what can be stored, so there is no need to modify the code that was generated by the Class Builder utility. • Next is the Length property, which stores the length of the string. The user should not be able to set this property, as it will depend entirely on the string stored in the Value property. By deleting the corresponding Property Let procedure, we will make Length a read-only property that can be read, but not set, by the user. We also need to add code that will calculate the length of the string in the Value property and store it in the Length property. • Finally, we have the Insert method. We need to write code to perform the string insertion. Okay, let’s get to work, dealing with the property procedures first. Select the SuperString class in the Project Explorer window and display its code for editing. Then, follow these steps: 1. Delete the entire Property Let Length procedure. 2. Add the following line of code at the beginning of the Property Get Length procedure (Len() is a built-in Basic function that returns the length of a string):
      mvarLength = Len(mvarValue)
3. Add the same line of code as the last statement in the Property Let Value procedure. Step 1, as I’ve already mentioned, serves the purpose of making the Length property read-only. Steps 2 and 3 add code that calculates the length of the string stored in the Value property and stores the result in the Length property. Why do we need to perform this calculation in two different places? The reason for putting this line of code in the Property Get Length procedure is obvious—to ensure that the Length property contains an accurate value when it is retrieved by the user. But why do we also need this code in the Property Let Value procedure? By doing so, the Length property will be updated each time the user changes the Value property, and therefore, will contain an accurate value even if the user has not specifically retrieved the Length property. The need for doing this will be obvious when we discuss the code for the Insert method. Now we need to turn our attention to the Insert method. The task of this method is to insert a new string at a specified position within the string currently stored. For example, if the SuperString object currently has “Visual Basic” stored in it, then calling Insert with the arguments “XXX” and 3 will result in the object having “ViXXXsual Basic” stored in it. Let’s be specific about what the code in the method needs to do, paying particular attention to special cases: • If the Position argument is equal to or less than 1, the new string is added at the beginning of the old string. • If Position is equal to or greater than the length of the old string, then the new string is added at the end of the old string. • If the old string is blank, then the new string simply replaces it. • If the new string is blank, nothing happens. • Otherwise, insert the new string at the specified position in the old string. The code for the Insert method is shown in Listing 6.2. You should be able to figure out how it works from the comments in the listing and from what you have learned already about Basic code. I will point out one thing: The code in this procedure needs to know the length of the original string stored in mvarValue, and it gets this information from the object’s Length property—in other words, the value stored in mvarLength. This is why we updated this value not only in the Property Get Length procedure, but also in the Property Let Value procedure. Otherwise, the Length property could be inaccurate. Listing 6.2 Code for the Insert method.
    Public Sub Insert(txt As String, position As Long)

    ‘ Inserts txt at position in mvarValue.

    Dim s1 As String, s2 As String

    ‘ If the new string is blank, exit.
    If txt = “” Then Exit Sub

     ‘ If the existing string is blank, replace it.
    If mvarValue = “” Then
        mvarValue = txt
        Exit Sub
    End If

    ‘ If Position is <= 1, tack new string on the beginning.
    If position <= 1 Then
        mvarValue = txt & mvarValue
        Exit Sub
    End If

    ‘ If Position equal or greater than the length
    ‘ of the existing string, tack the new string
    ‘ on at the end.

    If position >= mvarLength Then
        mvarValue = mvarValue & txt
        Exit Sub
    End If

    ‘ Otherwise stick it in the middle at the
    ‘ specified position.
    ‘ First get the left part of the old string up to but
    ‘ not including the character at position.
    s1 = Left(mvarValue, position - 1)
    ‘ Now get the right part of the old string from position
    ‘ to the end.
    s2 = Right(mvarValue, mvarLength - position + 1)
    ‘ Now put the pieces back together.
    mvarValue = s1 & txt & s2

    End Sub
We are almost done creating our SuperString class, but there’s one more thing to do—add initialization code. This code is placed in the class’s Initialize event procedure. This procedure is called when an instance of the object is first created, and you place code here to set the initial values of properties and perform other needed tasks. For the SuperString class, we will initialize the Value property to an empty string and the Length property to 0. Strictly speaking, these initialization steps are not needed, because Visual Basic automatically initializes string variables to an empty string and numeric variables to 0 when they are declared. Paying attention to class initialization steps is good programming practice, however, and you might as well start developing good habits early.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.